import time
import pytest
import logging
from .python_code.calculator import Calculator

"""
注意：文件所在目录得是一个package
"""


@pytest.fixture(scope="class")
def get_calc():
    calc = Calculator()
    # 相当于return，暂停并且记住当前位置。实现teardown的操作
    yield calc
    logging.info("结束测试")


@pytest.fixture(autouse=True)
def calc_fix():
    logging.info("开始计算")
    yield
    logging.info("结束计算")


def pytest_collection_modifyitems(items):
    """测试用例名称中文转码"""
    for item in items:
        item.name = item.name.encode('utf-8').decode('unicode-escape')
        item._nodeid = item.nodeid.encode('utf-8').decode('unicode-escape')


@pytest.fixture(scope="session", autouse=True)
def manage_logs(request):
    """Set log file name same as test name"""
    now = time.strftime("%Y-%m-%d %H-%M-%S")
    log_name = 'log/' + now + '.logs'

    request.config.pluginmanager.get_plugin("logging-plugin").set_log_path(log_name)
